package com.wx.oss.util;

import com.wx.oss.entity.FileInfo;
import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Bucket;
import io.minio.messages.Item;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @BelongsProject: xx-club
 * @BelongsPackage: com.wx.oss.util
 * @Author: wangx
 * @CreateTime: 2024-03-18  10:16
 * minio文件操作工具类
 * @Version: 1.0
 */
@Component
public class MinioUtil {

    @Resource
    private   MinioClient minioClient;

    /**
     * 创建桶
     */
    public void makeBucket(String bucketName) throws Exception{
        //先判断桶是否存在
        boolean exists = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
        if (!exists){
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }
    /**
     * 列出所有桶
     */
    public List<String> listBuckets() throws Exception{
        List<Bucket> buckets = minioClient.listBuckets();
        return buckets.stream().map(Bucket::name).collect(Collectors.toList());
    }

    /**
     * 列出当前桶及文件
     */
    public List<FileInfo> getAllFile(String bucketName) throws Exception{
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        ArrayList<FileInfo> fileList = new ArrayList<>();
        for (Result<Item> result : results){
            FileInfo fileInfo = new FileInfo();
            Item item = result.get();
            fileInfo.setFileName(item.objectName());
            fileInfo.setDir(item.isDir());
            fileInfo.setEtag(item.etag());
            fileList.add(fileInfo);
        }
        return fileList;
    }
    /**
     * 删除桶
     */
    public void removeBucket(String bucketName) throws Exception{
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }
    /**
     * 上传文件
     */
    public void uploadFile(InputStream inputStream, String bucketName, String fileName) throws Exception{
            minioClient.putObject(PutObjectArgs.builder()
                    .bucket(bucketName)
                    .object(fileName)
                    .stream(inputStream, -1,5242889L).build());
    }
    /**
     * 下载文件
     */
    public InputStream downloadFile(String bucketName, String fileName) throws Exception{
        return  minioClient
                .getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
    }
    /**
     *  删除文件
     */
    public void removeFile(String bucketName, String fileName) throws Exception{
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(fileName).build());
    }
    /**
     * 获取文件url
     */
    public String getFileUrl(String bucketName, String fileName) throws Exception{
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(fileName).build();
        return minioClient.getPresignedObjectUrl(args);
    }
}



